// pages/consultationOnline/index.js
const api = require('../../config/api')
const request = require('../../utils/request')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    getTypeList: [],
    questionList: [],
    tilast: '',
    height: '',
    isQuestionList: false,
    isType: true,
    QAlist: [],
    animationData: {},
    animationQuestionData: {},
    showList: 0,
    modalHeight: app.kBottomSafeHeight + 80,
    contentHeight: app.kBottomSafeHeight + 466,
    typeListLength: 0,
    sheetHeight: app.kBottomSafeHeight + 345,
    showModal: false
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      height: wx.getSystemInfoSync().windowHeight,
    })
    this.getList()
  },
  // 隐藏问题弹窗
  hideQuestionSheet() {
    this.setData({
      showModal: false,
      showList: 2,
      isQuestionList: false,
    })
  },
  clooseModel() {
    this.setData({
      showList: 2,
      isQuestionList: false,
      showModal: false,
    })
  },
  // 底部弹窗
  showQuestion() {
    const { QAlist, typeListLength} = this.data

    let query = wx.createSelectorQuery()
    query
      .select('#type')
      .boundingClientRect((res) => {
        this.setData({
          typeScrollTop: 0,
        })
        // res.top // 这个组件内 #the-id 节点的上边界坐标
      })
      .exec()

    query
      .select('#question')
      .boundingClientRect((res) => {
        this.setData({
          questionScrollTop: 0,
        })
        // res.top // 这个组件内 #the-id 节点的上边界坐标
      })
      .exec()

    query
      .select('#page')
      .boundingClientRect((res) => {
        this.setData({
          pageScroll: 99999999999,
        })
        // res.top // 这个组件内 #the-id 节点的上边界坐标
      })
      .exec()

    if(typeListLength > 1){
      this.setData({
        showList: 1,
        isQuestionList: true,
        QAlist,
      })
    }else{
      this.setData({
        showModal: true,
        isQuestionList: true,
        QAlist,
      })
    }
    
  },

  back() {
    let { typeListLength } = this.data
    let query = wx.createSelectorQuery()
    query
      .select('#type')
      .boundingClientRect((res) => {
        this.setData({
          typeScrollTop: 0,
        })
        // res.top // 这个组件内 #the-id 节点的上边界坐标
      })
      .exec()
    this.setData({
      showList: typeListLength > 1 ? 4 : 2,
      // isQuestionList: false,
    })
  },
  //获取咨询分类列表
  getList: async function () {
    const res = await request.get(api.getTypeList)
    if (res.httpStatus == 200) {
      let str = res.data
      this.setData({
        getTypeList: str,
        typeListLength: str.length
      })
      
      if(str.length === 1){
        const { dictKey, id } = str[0]
        const res = await request.get(api.getKeyword, {
          typeId: id,
        })
        if (res.httpStatus === 200) {
          this.setData({
            typeName: dictKey,
            questionList: res.data,
            
          })
        }
      }
    }
  },
  // 获取分类关键字
  getTypeItem: async function (val) {
    const { dictKey, id } = val.currentTarget.dataset.item
    const res = await request.get(api.getKeyword, {
      typeId: id,
    })
    if (res.httpStatus === 200) {
      this.setData({
        typeName: dictKey,
        questionList: res.data,
        showList: 3,
      })
    }
  },
  // 获取答案
  getAnswer: async function (val) {
    const { item } = val.currentTarget.dataset
    const res = await request.get(api.getKeywordById, {
      consultId: item.id,
    },{loading: false})
    const newlist = this.data.QAlist
    newlist.push({
      type: 'right',
      lable: item.consultName,
    })
    if (res.httpStatus === 200) {
      newlist.push(res.data)
      this.setData({
        QAlist: newlist,
        tilast: `item${newlist.length - 1}`,
        // isType: true,
        // isQuestionList: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
